<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class crontab_check_log_tongji extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'songshu:crontab_check_log_tongji {day?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计客服审核数量 day可选参数Ymd格式';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
        $current_day = $this->argument('day');
        if(empty($current_day)){
            $current_day = date('Ymd',strtotime("-1 day"));
        }
        $begin_time = strtotime($current_day);
        $end_time = strtotime($current_day.'235959');

        #$admin_ids = DB::table('check_log')->select('admin_id')->where('type',0)->distinct('admin_id')->whereBetween('add_time',[$begin_time,$end_time])->get()->toArray();
        $admin_ids = DB::table('check_log')->select('admin_id')->where('ymd',$current_day)->where('type',0)->distinct('admin_id')->get()->toArray();
        $admin_ids = get_array_keys($admin_ids,'admin_id');
        
        $admin_users = DB::table('admindata')->whereIn('id',$admin_ids)->get()->toArray();
        $admin_users = arrays_set_key($admin_users,'id');
         
        foreach($admin_ids as $admin_id){
            $articles = DB::table('check_log')->where('admin_id',$admin_id)->where('status',1)->select(DB::raw('count(id) as count,cat_id'))
            ->whereBetween('add_time',[$begin_time,$end_time])
            ->where('type',0)->groupBy('cat_id')->get()->toArray();
            foreach($articles as $item){
                $exists = DB::table('check_log_tongji')->where([
                    ['admin_id','=',$admin_id],
                    ['ymd','=',$current_day],
                    ['cat_id','=',$item->cat_id],
                    ['type','=',0]
                ])->exists();
                if($exists){
                    DB::table('check_log_tongji')->where([
                        ['admin_id','=',$admin_id],
                        ['ymd','=',$current_day],
                        ['cat_id','=',$item->cat_id],
                        ['type','=',0]
                    ])->update([
                        'total'=>$item->count,
                    ]);
                }else{
                    DB::table('check_log_tongji')->insert([
                        'admin_id'=>$admin_id,
                        'ymd'=>$current_day,
                        'ym'=>date('Ym',$begin_time),
                        'total'=>$item->count,
                        'cat_id'=>$item->cat_id,
                        'type'=>0,
                        'admin_name'=>$admin_users[$admin_id]->username??'unfind',
                    ]);
                }
            }
        }
        $this->video();
    }

    public function video(){
        $current_day = $this->argument('day');
        if(empty($current_day)){
            $current_day = date('Ymd');
        }
        $begin_time = strtotime($current_day);
        $end_time = strtotime($current_day.'235959');

        #$admin_ids = DB::table('check_log')->select('admin_id')->where('type',1)->distinct('admin_id')->whereBetween('add_time',[$begin_time,$end_time])->get()->toArray();
        $admin_ids = DB::table('check_log')->select('admin_id')->where('type',1)->distinct('admin_id')->where('ymd',$current_day)->get()->toArray();
        $admin_ids = get_array_keys($admin_ids,'admin_id');
        
        $admin_users = DB::table('admindata')->whereIn('id',$admin_ids)->get()->toArray();
        $admin_users = arrays_set_key($admin_users,'id');
         
        foreach($admin_ids as $admin_id){
            $articles = DB::table('check_log')->where('admin_id',$admin_id)->where('status',1)->select(DB::raw('count(id) as count,cat_id'))
            ->whereBetween('add_time',[$begin_time,$end_time])
            ->where('type',1)->groupBy('cat_id')->get()->toArray();
            foreach($articles as $item){
                $exists = DB::table('check_log_tongji')->where([
                    ['admin_id','=',$admin_id],
                    ['ymd','=',$current_day],
                    ['cat_id','=',$item->cat_id],
                    ['type','=',1]
                ])->exists();
                if($exists){
                    DB::table('check_log_tongji')->where([
                        ['admin_id','=',$admin_id],
                        ['ymd','=',$current_day],
                        ['cat_id','=',$item->cat_id],
                        ['type','=',1]
                    ])->update([
                        'total'=>$item->count,
                    ]);
                }else{
                    DB::table('check_log_tongji')->insert([
                        'admin_id'=>$admin_id,
                        'ymd'=>$current_day,
                        'ym'=>date('Ym',$begin_time),
                        'total'=>$item->count,
                        'cat_id'=>$item->cat_id,
                        'type'=>1,
                        'admin_name'=>$admin_users[$admin_id]->username??'unfind',
                    ]);
                }
            }
        }
    }

    /**
     * 弃用
     * [video 视频统计]
     */
    public function video1(){
        $current_day = $this->argument('day');
        if(empty($current_day)){
            $current_day = date('Ymd');
        }
        $begin_time = strtotime($current_day);
        $end_time = strtotime($current_day.'235959');

        $admin_ids = DB::table('check_log')->select('admin_id')->where('type',0)->distinct('admin_id')->whereBetween('add_time',[$begin_time,$end_time])->get()->toArray();
        $admin_ids = get_array_keys($admin_ids,'admin_id');
        
        $admin_users = DB::table('admindata')->whereIn('id',$admin_ids)->get()->toArray();
        $admin_users = arrays_set_key($admin_users,'id');
        foreach($admin_ids as $admin_id){
            $articles = DB::table('check_log')->where('admin_id',$admin_id)->select(DB::raw('count(id) as count,cat_id'))
            ->whereBetween('add_time',[$begin_time,$end_time])         
            ->where('type',1)->groupBy('cat_id')->get()->toArray();
            foreach($articles as $item){
                $exists = DB::table('check_log_tongji')->where([
                    ['admin_id','=',$admin_id],
                    ['ymd','=',$current_day],
                    ['cat_id','=',$item->cat_id],
                    ['type','=',1]
                ])->exists();
                if($exists){
                    DB::table('check_log_tongji')->where([
                        ['admin_id','=',$admin_id],
                        ['ymd','=',$current_day],
                        ['cat_id','=',$item->cat_id],
                        ['type','=',1]
                    ])->update([
                        'total'=>$item->count,
                    ]);
                }else{
                    DB::table('check_log_tongji')->insert([
                        'admin_id'=>$admin_id,
                        'ymd'=>$current_day,
                        'ym'=>date('Ym',$begin_time),
                        'total'=>$item->count,
                        'cat_id'=>$item->cat_id,
                        'type'=>1,
                        'admin_name'=>$admin_users[$admin_id]->username??'unfind',
                    ]);
                }
            }
        }
    }
}
